const sqlQuery = require("../mysql/index.cjs")
const encrypt = require("./encrypt.cjs")
const email = require("./email.cjs")
class login {
  constructor() { }
  static login(req, res) {
    let body = req.body
    // 准备sql语句
    let sql = `select * from user where email="${body.email}"`
    sqlQuery(sql, msg => {
      // 
      if (msg[0]) {
        if (body.password== msg[0].password) {
          res.send({
            status: 100,
            msg: "登录成功",
            data:msg
          })
        } else {
          res.send({
            status: 105,
            msg: "密码错误"
          })
        }
      } else {
        res.send({
          status: 102,
          msg: "没有该账号"
        })
      }
    })
  }
  static sign(req, res) {
    const body = req.body
    let sqlUser = `select * from user where email="${body.email}"`
    // 查询该邮箱是否已经注册
    sqlQuery(sqlUser, (msg) => {
      if (msg[0]) {
        res.send({
          status: 101,
          msg: "该邮箱已注册！"
        })
      } else {
        // 通过sql语句查询数据库中 是否存在与邮箱对应的验证码
        let sql = `select * from code where email="${body.email}" and code="${body.code}"`
        sqlQuery(sql, msg => {
          if (msg[0]) {
            let sql = `insert into user(name,email,password) values("${body.name}","${body.email}","${body.password}")`
            sqlQuery(sql, msg => {
              if (msg) {
                res.send({
                  status: 100,
                  msg: "注册成功！"
                })
              } else {
                res.send({
                  status: 104,
                  msg: "内部错误请稍后再试！"
                })
              }
            })
          } else {
            res.send({
              status: 103,
              msg: "验证码错误！"
            })
          }
        })
      }
    })
  }
  static code(req, res) {
    // 获取用户邮箱
    // console.log(req);
    let userEmail = req.body.email
    console.log(userEmail,typeof req.body.flg);
    let flg = req.body.flg  ? false : true
    // 准备mysql语句
    let sqlUser = `select * from user where email="${userEmail}"`
    sqlQuery(sqlUser, (msg) => {
      if (flg) {
        if (msg[0]) {
          res.send({
            status: 101,
            msg: "该邮箱已注册！"
          })
        } else {
          // 生成4位数随机验证码
          let code = Math.floor(Math.random() * 10000).toString().padEnd(4, "0")
          let sqlCode = `insert into code(email,code) values("${userEmail}",${code})`
          sqlQuery(sqlCode, msg => {
            if (msg) {
              email(userEmail, code, (err) => {
                if (err) {
                  console.log(err.message);
                  res.send({
                    status: 103,
                    msg: "邮件系统繁忙请稍后再试"
                  })
                } else {
                  res.send({
                    status: 100,
                    msg: "已发送验证码"
                  })
                }
              })
            } else {
              res.send({
                status: 104,
                msg: "内部错误请稍后再试"
              })
            }
          })
          // 3分钟后删除验证码
          setTimeout(() => {
            let sql = `delete from code where code="${code}" and email="${userEmail}"`
            sqlQuery(sql, msg => {
              console.log(msg);
            })
          }, 1000 * 60 * 3)
        }
      } else {
        if (!msg[0]) {
          res.send({
            status: 101,
            msg: "该邮箱没有注册！"
          })
        } else {
          // 生成4位数随机验证码
          let code = Math.floor(Math.random() * 10000).toString().padEnd(4, "0")
          let sqlCode = `insert into code(email,code) values("${userEmail}",${code})`
          sqlQuery(sqlCode, msg => {
            if (msg) {
              email(userEmail, code, (err) => {
                if (err) {
                  console.log(err.message);
                  res.send({
                    status: 103,
                    msg: "邮件系统繁忙请稍后再试"
                  })
                } else {
                  res.send({
                    status: 100,
                    msg: "已发送验证码"
                  })
                }
              })
            } else {
              res.send({
                status: 104,
                msg: "内部错误请稍后再试"
              })
            }
          })
          // 3分钟后删除验证码
          setTimeout(() => {
            let sql = `delete from code where code="${code}" and email="${userEmail}"`
            sqlQuery(sql, msg => {
              console.log(msg);
            })
          }, 1000 * 60 * 3)
        }
      }
    })
  }
  static reset(req,res){
    let body = req.body
    // 通过sql语句查询数据库中 是否存在与邮箱对应的验证码
    let sql = `select * from code where email="${body.email}" and code="${body.code}"`
    sqlQuery(sql, msg => {
      if (msg[0]) {
        let sql = `update user set password="${body.password}" where email="${body.email}"`
        sqlQuery(sql, msg => {
          if (msg) {
            res.send({
              status: 100,
              msg: "修改成功！"
            })
          } else {
            res.send({
              status: 104,
              msg: "内部错误请稍后再试！"
            })
          }
        })
      } else {
        res.send({
          status: 103,
          msg: "验证码错误！"
        })
      }
    })
  }
}
module.exports = login